package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.dto.FilesRecentlyDto;
import cn.cdeden.knowledge.domain.dto.FilesRecentlyViewedQueryParam;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.cdeden.system.api.model.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.knowledge.mapper.FilesRecentlyViewedMapper;
import cn.cdeden.knowledge.domain.FilesRecentlyViewed;
import cn.cdeden.knowledge.service.FilesRecentlyViewedService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FilesRecentlyViewedServiceImpl extends ServiceImpl<FilesRecentlyViewedMapper, FilesRecentlyViewed> implements FilesRecentlyViewedService{

    private final FilesRecentlyViewedMapper filesRecentlyViewedMapper;

    @Override
    public boolean deleteByIds(Set<String> ids, String type) {
        LambdaQueryWrapper<FilesRecentlyViewed> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(FilesRecentlyViewed::getType,type);
        wrapper.in(FilesRecentlyViewed::getFilesId,ids);
        return remove(wrapper);
    }

    @Override
    public TableDataInfo<FilesRecentlyDto> queryAll(FilesRecentlyViewedQueryParam query, Pageable pageable) {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser();
        new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        IPage<FilesRecentlyDto> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FilesRecentlyDto> page = filesRecentlyViewedMapper.selectByExample(String.valueOf(user.getUserId()),query.getLibraryType(),queryPage);

        return TableDataInfo.build(page);
    }

    //企业文件查看
    @Override
    public TableDataInfo<FilesRecentlyDto> queryEnterpriseFile(FilesRecentlyViewedQueryParam query,Pageable pageable){
        IPage<FilesRecentlyDto> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FilesRecentlyDto> page = filesRecentlyViewedMapper.queryEnterpriseFileCheck(query.getFilesId().toString(), queryPage);
        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), FilesRecentlyDto.class);
    }


    //团队文件查看
    @Override
    public TableDataInfo<FilesRecentlyDto> queryTeamFile(FilesRecentlyViewedQueryParam query, Pageable pageable){
        IPage<FilesRecentlyDto> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FilesRecentlyDto> page = filesRecentlyViewedMapper.queryTeamFileCheck(query.getFilesId().toString(), queryPage);
        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), FilesRecentlyDto.class);
    }

}
